#include <stdio.h>
#include <glib-object.h>

#include <thrift/c_glib/protocol/thrift_binary_protocol.h>
#include <thrift/c_glib/transport/thrift_buffered_transport.h>
#include <thrift/c_glib/transport/thrift_socket.h>

#include "gen-c_glib/dmm.h"

int main (void)
{
    ThriftSocket *socket;
    ThriftTransport *transport;
    ThriftProtocol *protocol;
    dmmIf *client;
    ParameterValue_t *value;
    GError *error = NULL;
    gint32 result;
    int exit_status = 0;
    hello_world_t *param1, *param2;
    GPtrArray *send;	
    GPtrArray *resp;

#if (!GLIB_CHECK_VERSION (2, 36, 0))
    g_type_init ();
#endif

    socket    = g_object_new (THRIFT_TYPE_SOCKET, "hostname",  "localhost", "port", 9010, NULL);
    transport = g_object_new (THRIFT_TYPE_BUFFERED_TRANSPORT, "transport", socket, NULL);
    protocol  = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport", transport, NULL);

    thrift_transport_open (transport, &error);


    client = g_object_new (TYPE_DMM_CLIENT, "input_protocol",  protocol, "output_protocol", protocol, NULL);
    
    value = g_object_new(TYPE_PARAMETER_VALUE_T, NULL); 

    g_object_set (value,
                            "name", "aaa",
                            "paramkey",   "bbb",
                            "val",    "ccc", NULL);
    if (!error && dmm_if_set_parameter_value (client, &result, value, &error)) {
        puts ("C:set success\n");
    }

    if (!error && dmm_if_get_parameter_value (client, &value, &error)) {
        printf ("C:get success, name:[%s], key:[%s], val:[%s]\n", value->name, value->paramkey, value->val);
    }

    param1 = g_object_new(TYPE_HELLO_WORLD_T, "response", "set", NULL);
    param2 = g_object_new(TYPE_HELLO_WORLD_T, "response", "have", NULL);
    send = g_ptr_array_new();
    g_ptr_array_add(send, param1);
    g_ptr_array_add(send, param2);

    resp = g_ptr_array_new();	

    if (!error && dmm_if_gptrarray_test (client, &resp, send, &error))
    {
        puts("C:get gptrarray respone from server!\n");
    }

    if (error) {
        printf ("ERROR: %s\n", error->message);
        g_clear_error (&error);
        exit_status = 1;
    }

    thrift_transport_close (transport, NULL);

    g_object_unref (value);
    g_object_unref (client);
    g_object_unref (protocol);
    g_object_unref (transport);
    g_object_unref (socket);

    return exit_status;
}

